﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using X.Core.Interfaces;

namespace X.Core.Utils
{
    public class Util
    {
        //事件聚合器
        public static void ModelGuidJudge(Dictionary<Guid, int> dict, IModel model)
        {
            if (model.Id == Guid.Empty) model.Id = Guid.NewGuid();
            if (dict.ContainsKey(model.Id))
            {
                while (true)
                {
                    model.Id = Guid.NewGuid();
                    if (dict.ContainsKey(model.Id) == false)
                    {
                        dict.Add(model.Id, 0);
                        break;
                    }
                }
            }
            else
            {
                dict.Add(model.Id, 0);
            }
        }
        public static uint GetDuration(uint start, uint now)
        {
            uint duration = 0;

            if (now >= start)
            {
                duration = now - start;
                //Environment.TickCount
            }
            else
            {
                uint tmp = uint.MaxValue - start;
                duration = tmp + now;
            }
            return duration;
        }
        public static uint GetDuration(uint start)
        {
            return GetDuration(start, GetTickCount());
        }
        [DllImport("kernel32.dll", CallingConvention = CallingConvention.Winapi)]
        public extern static uint GetTickCount();
    }
}
